package cn.com.zonesion.rfiddemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bin.david.form.core.SmartTable;


import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import cn.com.zonesion.rfiddemo.R;
import cn.com.zonesion.rfiddemo.bean.UserAttendance;

public class HistoryAttendanceActivity extends AppCompatActivity {

    private  ArrayList<UserAttendance> attendance;
    private SmartTable smartTable;//第三方表格控件，用来展示员工的打卡时间以及打卡情况
    private TextView textView;//当没有打卡信息的时候，用来提示用户当前没有打卡信息
    private MenuItem menuItem;
    /**
     * handler用来定时发送消息获取系统时间
     */
    private AttendanceHandler handler;
    /**
     * 通过Handler发送消息获取系统时间的标识
     */
    private static final int GET_SYSTEM_TIME = 0;
    /**
     * 点击该按钮清除全部历史打卡记录
     */
    private Button btnDeleteAllClock;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_attendance);
        smartTable = findViewById(R.id.table);
        textView = findViewById(R.id.textView);
        btnDeleteAllClock = findViewById(R.id.btn_delete_all_clock);
        handler = new AttendanceHandler(this);
        //判断MainActivity传递过来的Intent对象是否为null 如果不是null，得到ArrayList<UserAttendance>对象
        if(getIntent() != null) {
           attendance = (ArrayList<UserAttendance>) getIntent().getSerializableExtra("attendance");
        }
        //判断得到的ArrayList<UserAttendance>对象是否为null，不为null，则判断是否有数据
        if(attendance != null){
            if(attendance.size() == 0){
                //没有数据则提示用户无打卡信息，并将表格控件和按钮控件隐藏
                textView.setVisibility(View.VISIBLE);
                smartTable.setVisibility(View.GONE);
                btnDeleteAllClock.setVisibility(View.GONE);
            }else{
                //有数据则隐藏提示文本，显示表格控件和按钮控件
                textView.setVisibility(View.GONE);
                btnDeleteAllClock.setVisibility(View.VISIBLE);
                smartTable.setVisibility(View.VISIBLE);
                //为表格控件设置数据
                smartTable.setData(attendance);
                //设置表格控件支持缩放
                smartTable.setZoom(true);
            }
        }
        //为btnDeleteAllClock注册单击事件
        btnDeleteAllClock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attendance.clear();//清空ArrayList的数据
                System.out.println("attendance == " + attendance);
                if(attendance.size() == 0){
                    //清除记录成功之后，重新为SmartTable设置数据并更新
                    Toast.makeText(getApplicationContext(), "记录清除成功", Toast.LENGTH_SHORT).show();
                    textView.setVisibility(View.VISIBLE);
                    smartTable.setVisibility(View.GONE);
                    btnDeleteAllClock.setVisibility(View.GONE);
                    Intent data = new Intent();
                    data.putExtra("clearAll",true);
                    setResult(Activity.RESULT_OK,data);
                }else{
                    Toast.makeText(getApplicationContext(), "记录清除失败，点击重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        //获取到选项菜单的第一项，赋值给menuItem，以便动态修改其内容
        menuItem = menu.getItem(0);
        //当OptionsMenu创建时发送消息查询当前系统时间并显示
        handler.sendEmptyMessage(GET_SYSTEM_TIME);
        return super.onCreateOptionsMenu(menu);
    }


    private static class AttendanceHandler extends Handler {
        //创建弱引用，防止内存泄露
        private WeakReference<HistoryAttendanceActivity> reference;

        AttendanceHandler(HistoryAttendanceActivity mainActivity) {
            //在构造方法中对reference初始化
            reference = new WeakReference<>(mainActivity);
        }


        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HistoryAttendanceActivity.GET_SYSTEM_TIME:
                    if (reference.get() != null) {
                        reference.get().menuItem.setTitle(new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date(System.currentTimeMillis())));
                    }
                    removeMessages(HistoryAttendanceActivity.GET_SYSTEM_TIME);
                    sendEmptyMessageDelayed(HistoryAttendanceActivity.GET_SYSTEM_TIME, 1000);
            }
        }
    }


}
